package cn.sjtu.java_util_concurrent.lock;

import java.util.concurrent.*;

/**
 * @author syo 2319615164@qq.com
 * @description 线程创建的方式
 * @date 2024/6/12
 * @Copyright syo
 */
public class TestCreateThread {
    public static void main(String[] args) {
        // 1. 通过匿名类
        new Thread() {
            @Override
            public void run() {
                System.out.println("创建了一个线程！");
            }
        }.start();

        // 2. 通过传递任务对象
        Runnable task = () -> System.out.println("创建了一个线程！");
        new Thread(task).start();

        // 3. 通过Callable + FutureTask
        FutureTask<String> futureTask = new FutureTask<>(new Callable() {
            @Override
            public String call() throws Exception {
                return "hello world";
            }
        });
        new Thread(futureTask).start();

        // 4. 通过线程池
        ExecutorService executorService = new ThreadPoolExecutor(
                1,
                2,
                30,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        // 5. 通过线程池
        ExecutorService executor = Executors.newSingleThreadExecutor();
    }

}
